﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
using Arction.CustomControls.Trader.WinForms;
using Arction.CustomControls.Trader.WinForms.TechnicalIndicators;
using HE.Utility.Universals;

namespace EvTest.ArctionTrade
{
    public partial class TradeX : Form
    {
        private string defualtcode = "000001";

        public TradeX()
        {
            InitializeComponent();
        }
        private void initForm()
        {
            this.Text = "股票代码：" + defualtcode;

            // 1. 初始化1支股票，获得当天交易信息
            // https://blog.csdn.net/zengcq2016/article/details/99682176
            // var hq_str_sz000001="平安银行,18.450,18.460,18.470,18.550,18.160,18.460,18.470,98028118,1798691052.860,199500,18.460,181000,18.450,31100,18.440,58700,18.430,128800,18.420,27138,18.470,551730,18.480,910000,18.490,1522922,18.500,130700,18.510,2021-12-08,15:00:03,00";

            var dayurl = "http://hq.sinajs.cn/list=sz" + defualtcode;

            WebClient wc = new WebClient();
            string day = wc.GetHtml(dayurl);
            //MessageBox.Show(day); //验证获得了数据√
            Regex myRegex = new Regex("var[^=]+=\"", RegexOptions.IgnoreCase);
            string[] ddata = myRegex.Replace(day, "").Replace("\"; ", "").Split(',');

            label1.Text = "股票名字：" + ddata[0];
            label2.Text = "今日开盘价：" + ddata[1];
            label3.Text = "昨日收盘价：" + ddata[2];
            label4.Text = "当前价格：" + ddata[3];
            label5.Text = "今日最高价：" + ddata[4];
            label6.Text = "今日最低价：" + ddata[5];
            label7.Text = "买一报价：" + ddata[6];
            label8.Text = "卖一报价：" + ddata[7];
            label9.Text = "成交的股票数：" + ddata[8];
            label10.Text = "成交金额：" + ddata[9];

            // 更多成交信息和监听方法，获得实时交易信息参考上方链接




            // 2. 获得股票历史中的交易信息
            // https://q.stock.sohu.com/hisHq?code=cn_601766,cn_000002&start=20210101&end=20210312
            // [{"status":0,"hq":[["2021-03-12","6.50","6.69","0.19","2.92%","6.42","6.76","1669529","110610.25","0.69%"],["2021-03-11","6.41","6.50","0.09","1.40%","6.41","6.53","972600","62947.88","0.40%"],["2021-03-10","6.52","6.41","-0.07","-1.08%","6.40","6.62","844529","54825.50","0.35%"],["2021-03-09","6.61","6.48","-0.13","-1.97%","6.30","6.70","1458739","94941.70","0.60%"],["2021-03-08","6.70","6.61","-0.06","-0.90%","6.60","6.82","1214779","81362.79","0.50%"],["2021-03-05","6.61","6.67","0.00","0.00%","6.60","6.82","1188462","79757.51","0.49%"],["2021-03-04","6.83","6.67","-0.27","-3.89%","6.67","6.86","1638671","110552.12","0.67%"],["2021-03-03","6.90","6.94","0.05","0.73%","6.78","7.04","1478852","102310.23","0.61%"],["2021-03-02","7.09","6.89","-0.21","-2.96%","6.81","7.09","1763822","121986.24","0.73%"],["2021-03-01","6.91","7.10","0.24","3.50%","6.74","7.15","2248226","156332.11","0.92%"],["2021-02-26","7.12","6.86","-0.43","-5.90%","6.84","7.17","2802882","195577.86","1.15%"],["2021-02-25","7.16","7.29","0.18","2.53%","7.06","7.42","3551148","258151.05","1.46%"],["2021-02-24","6.77","7.11","0.35","5.18%","6.69","7.25","2907593","201648.81","1.20%"],["2021-02-23","6.84","6.76","-0.17","-2.45%","6.72","7.18","2673197","185643.75","1.10%"],["2021-02-22","7.01","6.93","0.07","1.02%","6.84","7.34","4140166","293798.44","1.70%"],["2021-02-19","6.23","6.86","0.62","9.94%","6.21","6.86","3260819","214931.72","1.34%"],["2021-02-18","6.42","6.24","0.10","1.63%","6.23","6.47","1541404","97477.71","0.63%"],["2021-02-10","6.18","6.14","-0.05","-0.81%","6.10","6.21","1130547","69406.66","0.46%"],["2021-02-09","5.96","6.19","0.25","4.21%","5.95","6.20","1650068","101206.13","0.68%"],["2021-02-08","5.95","5.94","-0.02","-0.34%","5.86","6.05","1075763","64056.96","0.44%"],["2021-02-05","6.01","5.96","-0.09","-1.49%","5.96","6.11","1074392","64666.36","0.44%"],["2021-02-04","6.00","6.05","0.04","0.67%","5.90","6.12","1585927","95242.93","0.65%"],["2021-02-03","6.21","6.01","-0.26","-4.15%","6.00","6.23","1984198","120405.45","0.82%"],["2021-02-02","6.25","6.27","0.04","0.64%","6.18","6.42","1624283","102478.88","0.67%"],["2021-02-01","6.20","6.23","-0.02","-0.32%","6.12","6.30","1324846","82384.25","0.54%"],["2021-01-29","6.53","6.25","-0.24","-3.70%","6.15","6.56","2164372","136424.61","0.89%"],["2021-01-28","6.64","6.49","-0.27","-3.99%","6.45","6.65","2026280","132660.77","0.83%"],["2021-01-27","6.51","6.76","0.21","3.21%","6.46","6.93","3590382","242703.03","1.48%"],["2021-01-26","6.51","6.55","-0.03","-0.46%","6.36","6.59","2306221","149368.97","0.95%"],["2021-01-25","6.62","6.58","0.02","0.30%","6.45","6.75","2701754","177888.95","1.11%"],["2021-01-22","6.88","6.56","-0.38","-5.48%","6.53","6.88","3461191","230693.91","1.42%"],["2021-01-21","6.82","6.94","0.07","1.02%","6.65","7.02","3856219","263098.12","1.59%"],["2021-01-20","6.79","6.87","0.09","1.33%","6.63","7.11","4235828","291191.31","1.74%"],["2021-01-19","7.18","6.78","-0.50","-6.87%","6.75","7.35","5096582","356924.75","2.09%"],["2021-01-18","6.96","7.28","0.14","1.96%","6.95","7.44","5341188","384891.97","2.20%"],["2021-01-15","7.80","7.14","-0.49","-6.42%","6.94","7.80","8904252","654360.50","3.66%"],["2021-01-14","7.00","7.63","0.69","9.94%","6.87","7.63","7922107","588822.50","3.26%"],["2021-01-13","6.23","6.94","0.63","9.98%","6.22","6.94","7304638","492553.06","3.00%"],["2021-01-12","6.16","6.31","0.17","2.77%","6.12","6.45","3300926","209354.91","1.36%"],["2021-01-11","5.90","6.14","0.25","4.24%","5.81","6.33","3930888","242600.03","1.62%"],["2021-01-08","5.98","5.89","-0.25","-4.07%","5.86","6.03","2766081","164154.88","1.14%"],["2021-01-07","6.01","6.14","0.25","4.24%","5.84","6.36","6043383","368855.44","2.48%"],["2021-01-06","5.44","5.89","0.54","10.09%","5.41","5.89","3463655","197795.41","1.42%"],["2021-01-05","5.25","5.35","0.07","1.33%","5.25","5.70","2868949","157079.20","1.18%"],["2021-01-04","5.27","5.28","-0.03","-0.56%","5.24","5.30","787491","41507.21","0.32%"]],"code":"cn_601766"},{"status":0,"hq":[["2021-03-12","31.10","31.49","0.34","1.09%","30.66","32.36","901960","283421.91","0.93%"],["2021-03-11","30.76","31.15","0.46","1.50%","30.68","31.46","742679","230527.05","0.76%"],["2021-03-10","31.17","30.69","-0.33","-1.06%","30.30","31.20","877518","269562.69","0.90%"],["2021-03-09","31.32","31.02","-0.18","-0.58%","30.71","31.66","943415","295232.16","0.97%"],["2021-03-08","31.90","31.20","-0.34","-1.08%","31.20","32.20","771960","244055.83","0.79%"],["2021-03-05","32.20","31.54","-1.15","-3.52%","31.08","32.69","1303284","414188.91","1.34%"],["2021-03-04","33.30","32.69","-0.81","-2.42%","32.47","34.10","1292858","428282.69","1.33%"],["2021-03-03","32.95","33.50","0.21","0.63%","32.59","33.99","1164841","387675.03","1.20%"],["2021-03-02","33.00","33.29","-0.06","-0.18%","32.90","34.60","1220150","411031.56","1.26%"],["2021-03-01","33.10","33.35","0.25","0.76%","32.29","33.71","1280834","422975.09","1.32%"],["2021-02-26","32.57","33.10","0.11","0.33%","31.62","34.08","2529180","832930.75","2.60%"],["2021-02-25","30.49","32.99","3.00","10.00%","30.41","32.99","2595404","838266.38","2.67%"],["2021-02-24","29.92","29.99","-0.02","-0.07%","29.50","30.58","969948","291517.72","1.00%"],["2021-02-23","29.69","30.01","0.28","0.94%","29.29","30.05","861380","255868.75","0.89%"],["2021-02-22","29.84","29.73","-0.11","-0.37%","29.52","30.33","852341","254898.52","0.88%"],["2021-02-19","29.70","29.84","-0.17","-0.57%","29.60","30.04","758575","226094.11","0.78%"],["2021-02-18","28.95","30.01","1.18","4.09%","28.95","30.13","996922","295698.88","1.03%"],["2021-02-10","28.61","28.83","0.08","0.28%","28.35","28.98","652149","187496.88","0.67%"],["2021-02-09","28.99","28.75","-0.38","-1.30%","28.54","29.07","673568","193433.05","0.69%"],["2021-02-08","29.00","29.13","0.25","0.87%","28.60","29.76","832037","242805.53","0.86%"],["2021-02-05","28.01","28.88","0.99","3.55%","28.01","29.34","1030261","295483.72","1.06%"],["2021-02-04","27.89","27.89","-0.03","-0.11%","27.43","28.35","665574","185606.25","0.69%"],["2021-02-03","28.34","27.92","-0.42","-1.48%","27.78","28.35","589610","164669.28","0.61%"],["2021-02-02","28.19","28.34","0.27","0.96%","27.96","28.48","716108","202128.81","0.74%"],["2021-02-01","27.97","28.07","0.28","1.01%","27.26","28.12","886694","245662.19","0.91%"],["2021-01-29","28.55","27.79","-0.64","-2.25%","27.57","28.65","963685","269172.75","0.99%"],["2021-01-28","29.05","28.43","-0.97","-3.30%","28.22","29.07","786041","224053.45","0.81%"],["2021-01-27","29.69","29.40","-0.30","-1.01%","29.37","29.95","724303","214677.25","0.75%"],["2021-01-26","29.26","29.70","0.34","1.16%","29.15","30.15","816655","243326.64","0.84%"],["2021-01-25","29.26","29.36","0.10","0.34%","28.56","29.60","973750","282586.78","1.00%"],["2021-01-22","29.95","29.26","-0.84","-2.79%","29.01","29.95","1058090","311413.75","1.09%"],["2021-01-21","30.30","30.10","-0.40","-1.31%","30.02","30.70","875253","265078.16","0.90%"],["2021-01-20","31.07","30.50","-0.78","-2.49%","30.35","31.20","961435","294692.06","0.99%"],["2021-01-19","31.19","31.28","0.02","0.06%","30.72","32.35","1631688","513023.28","1.68%"],["2021-01-18","30.20","31.26","1.31","4.37%","29.83","31.26","1258282","384731.53","1.30%"],["2021-01-15","30.01","29.95","-0.04","-0.13%","29.88","30.73","1123372","339907.25","1.16%"],["2021-01-14","29.93","29.99","0.09","0.30%","29.80","30.79","1216323","368193.81","1.25%"],["2021-01-13","29.90","29.90","0.20","0.67%","29.60","30.22","940504","280791.69","0.97%"],["2021-01-12","29.78","29.70","-0.08","-0.27%","29.37","29.80","980654","289907.19","1.01%"],["2021-01-11","29.50","29.78","0.44","1.50%","29.27","30.35","1388121","414248.28","1.43%"],["2021-01-08","28.98","29.34","0.55","1.91%","28.81","29.45","1028563","300084.56","1.06%"],["2021-01-07","29.00","28.79","0.04","0.14%","28.39","29.50","1226756","354422.41","1.26%"],["2021-01-06","27.80","28.75","0.84","3.01%","27.73","28.75","1048801","297257.28","1.08%"],["2021-01-05","27.78","27.91","0.13","0.47%","27.10","27.93","1162658","318960.72","1.20%"],["2021-01-04","28.40","27.78","-0.92","-3.21%","27.52","28.40","1468441","407941.69","1.51%"]],"code":"cn_000002"}]

            string yearsurl = "https://q.stock.sohu.com/hisHq?code=cn_" + defualtcode + "&start=" + dtp1.Value.ToString("yyyyMMdd") + "&end=" + dtp2.Value.ToString("yyyyMMdd");
            string years = wc.GetHtml(yearsurl);
            //MessageBox.Show(years);//验证获得了数据√
            JavaScriptSerializer js = new JavaScriptSerializer();
            //https://www.json.cn/json/json2csharp.html  获得实体类的反序列化结构

            ToJsonMy[] ms = js.Deserialize<ToJsonMy[]>(years);

            // 把每日数据输出来  ["2021-03-12","6.50","6.69","0.19","2.92%","6.42","6.76","1669529","110610.25","0.69%"]
            List<List<string>> fromdata = ms[0].hq;

            if (fromdata == null)
            {
                MessageBox.Show("未能获得历史信息");
                return;
            }


            // Disable rendering
            chart.BeginUpdate();
            //chart.ChartName = "SimpleChart";

            // Add TradingChart to a parent container
            chart.Parent = splitContainer1.Panel1;
            chart.Dock = DockStyle.Fill;

            // Configuring appearance
            chart.SetAppearance(Arction.CustomControls.Trader.WinForms.Appearance.Blue);
            chart.ShowSearchBar = true;
            chart.ShowTimeRangeSelection = true;

            // Creating technical indicators and adding them to the chart
            SimpleMovingAverage sma = new SimpleMovingAverage();
            sma.LineWidth = 1.5;
            sma.PeriodCount = 10;
            sma.Title = "Simple Moving Average";
            chart.Indicators.Add(sma);

            Volume volume = new Volume();
            volume.Color = Color.Aqua;
            chart.Indicators.Add(volume);

            // Read trading data from a file
            OhlcData[] data = new OhlcData[fromdata.Count];

            // 3. 按照lightning chart的规则组装K线图交易数据源, 见说明书6.14.1 StockSeries 的数据设置
            var i = fromdata.Count - 1;
            foreach (List<string> s in fromdata)
            {
                DateTime d = Convert.ToDateTime(s[0] + " 00:00:00");
                OhlcData one = new OhlcData();
                one.DateTime = d;
                one.Open = s[1].ToDouble();//开市价格
                one.High = s[6].ToDouble();//最高
                one.Low = s[5].ToDouble();// 最低
                one.Close = s[2].ToDouble();// 收市价格
                one.Volume = (UInt32)s[7].ToInt32();//交易量         

                data[i] = one;
                i--;
            }

            // Set data to the chart
            chart.SetData(data, "AAPL", "何大大自定义数据源");

            // Enable rendering
            chart.EndUpdate();


        }

        private void button1_Click(object sender, EventArgs e)
        {

            this.defualtcode = textBox1.Text.Trim();
            initForm();
        }

        private void chart_Load(object sender, EventArgs e)
        {

        }

        private void TradeX_Load(object sender, EventArgs e)
        {
            dtp1.Value = DateTime.Now.AddYears(-2);

            initForm();

        }
    }
}
